#include "Atmosphere.h"
/*
void AtmMgr::InitResources() {

	char *cs_ver = "cs_5_0";

	ID3D11Texture2D *tex2D;	
	D3D11_TEXTURE2D_DESC tdesc2d;
	D3D11_SHADER_RESOURCE_VIEW_DESC srvdesc;
	D3D11_UNORDERED_ACCESS_VIEW_DESC uavdesc;

	ID3D11Texture3D	*tex3D;
	D3D11_TEXTURE3D_DESC tdesc3d;
	//========================================================
	//				Transmittance
	//========================================================

	memset( &tdesc2d, 0, sizeof(tdesc2d) );
	tdesc2d.ArraySize = 1;
	tdesc2d.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
	tdesc2d.CPUAccessFlags = 0;
	tdesc2d.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
	tdesc2d.Height = TRANSMITTANCE_H;
	tdesc2d.Width = TRANSMITTANCE_W;
	tdesc2d.MipLevels = 1;
	tdesc2d.MiscFlags = 0;
	tdesc2d.SampleDesc.Count = 1;
	tdesc2d.Usage = D3D11_USAGE_DEFAULT;
	HR( Dev->CreateTexture2D( &tdesc2d, NULL, &tex2D ) );

	memset( &srvdesc, 0, sizeof(srvdesc) );
	srvdesc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
	srvdesc.Texture2D.MipLevels = 1;
	srvdesc.Texture2D.MostDetailedMip = 0;
	srvdesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
	HR( Dev->CreateShaderResourceView( tex2D, &srvdesc, &Transmittance ) );

	memset( &uavdesc, 0, sizeof(uavdesc) );
	uavdesc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
	uavdesc.Texture2D.MipSlice = 0;
	HR( Dev->CreateUnorderedAccessView( tex3D, &uavdesc, &Transmittance_UAV ) );

	::CompileFromFile( "Modules\\D3D11Shaders\\PAS_Transmittance.fx", NULL, NULL, "CS_Transmittance", cs_ver, ShaderCompileFlag, 0, NULL, &SBlob, &EBlob, &hr );
	ShowShaderCompilationError( hr, EBlob );
	HR( Dev->CreateComputeShader( SBlob->GetBufferPointer(), SBlob->GetBufferSize(), NULL, &CS_Transmittance ) );
	REL( SBlob );
	REL( EBlob );

	//========================================================
	//				Irradiance	[merge with deltaE]
	//========================================================

	//========================================================
	//				Inscatter
	//========================================================

	memset( &tdesc3d, 0, sizeof(tdesc3d) );
	tdesc3d.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
	tdesc3d.CPUAccessFlags = 0;
	tdesc3d.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
	tdesc3d.Width = RES_MU_S*RES_NU;
	tdesc3d.Height = RES_MU;
	tdesc3d.Depth = RES_R;
	tdesc3d.MipLevels = 1;
	tdesc3d.MiscFlags = 0;
	tdesc3d.Usage = D3D11_USAGE_DEFAULT;

	HR( Dev->CreateTexture3D( &tdesc3d, NULL, &tex3D ) );

	memset( &srvdesc, 0, sizeof(srvdesc) );
	srvdesc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
	srvdesc.Texture3D.MipLevels = 1;
	srvdesc.Texture3D.MostDetailedMip = 0;
	srvdesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;

	HR( Dev->CreateShaderResourceView( tex3D, &srvdesc, &Inscatter ) );

	::CompileFromFile( "Modules\\D3D11Shaders\\PAS_Inscatter1.fx", NULL, NULL, "CS_Inscatter", cs_ver, ShaderCompileFlag, 0, NULL, &SBlob, &EBlob, &hr );
	ShowShaderCompilationError( hr, EBlob );
	HR( Dev->CreateComputeShader( SBlob->GetBufferPointer(), SBlob->GetBufferSize(), NULL, &CS_Transmittance ) );
	REL( SBlob );
	REL( EBlob );
	//					NEW++++++
	//========================================================
	//				IrradianceN
	//========================================================

	memset( &tdesc3d, 0, sizeof(tdesc3d) );
	tdesc3d.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
	tdesc3d.CPUAccessFlags = 0;
	tdesc3d.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
	tdesc3d.Width = RES_MU_S*RES_NU;
	tdesc3d.Height = RES_MU;
	tdesc3d.Depth = RES_R;
	tdesc3d.MipLevels = 1;
	tdesc3d.MiscFlags = 0;
	tdesc3d.Usage = D3D11_USAGE_DEFAULT;

	HR( Dev->CreateTexture3D( &tdesc3d, NULL, &tex3D ) );

	memset( &srvdesc, 0, sizeof(srvdesc) );
	srvdesc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
	srvdesc.Texture3D.MipLevels = 1;
	srvdesc.Texture3D.MostDetailedMip = 0;
	srvdesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;

	HR( Dev->CreateShaderResourceView( tex3D, &srvdesc, &DeltaJ ) );

	::CompileFromFile( "Modules\\D3D11Shaders\\PAS_InscatterN.fx", NULL, NULL, "CS_copyInscatterN", cs_ver, ShaderCompileFlag, 0, NULL, &SBlob, &EBlob, &hr );
	ShowShaderCompilationError( hr, EBlob );
	HR( Dev->CreateComputeShader( SBlob->GetBufferPointer(), SBlob->GetBufferSize(), NULL, &CS_DeltaJ ) );
	REL( SBlob );
	REL( EBlob );

	//========================================================
	//				InscatterN
	//========================================================

	memset( &tdesc3d, 0, sizeof(tdesc3d) );
	tdesc3d.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
	tdesc3d.CPUAccessFlags = 0;
	tdesc3d.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
	tdesc3d.Width = RES_MU_S*RES_NU;
	tdesc3d.Height = RES_MU;
	tdesc3d.Depth = RES_R;
	tdesc3d.MipLevels = 1;
	tdesc3d.MiscFlags = 0;
	tdesc3d.Usage = D3D11_USAGE_DEFAULT;

	HR( Dev->CreateTexture3D( &tdesc3d, NULL, &tex3D ) );

	memset( &srvdesc, 0, sizeof(srvdesc) );
	srvdesc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
	srvdesc.Texture3D.MipLevels = 1;
	srvdesc.Texture3D.MostDetailedMip = 0;
	srvdesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;

	HR( Dev->CreateShaderResourceView( tex3D, &srvdesc, &DeltaJ ) );

	::CompileFromFile( "Modules\\D3D11Shaders\\PAS_InscatterN.fx", NULL, NULL, "CS_copyInscatterN", cs_ver, ShaderCompileFlag, 0, NULL, &SBlob, &EBlob, &hr );
	ShowShaderCompilationError( hr, EBlob );
	HR( Dev->CreateComputeShader( SBlob->GetBufferPointer(), SBlob->GetBufferSize(), NULL, &CS_DeltaJ ) );
	REL( SBlob );
	REL( EBlob );

	//========================================================
	//				copyInscatterN
	//========================================================

	memset( &tdesc3d, 0, sizeof(tdesc3d) );
	tdesc3d.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS;
	tdesc3d.CPUAccessFlags = 0;
	tdesc3d.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
	tdesc3d.Width = RES_MU_S*RES_NU;
	tdesc3d.Height = RES_MU;
	tdesc3d.Depth = RES_R;
	tdesc3d.MipLevels = 1;
	tdesc3d.MiscFlags = 0;
	tdesc3d.Usage = D3D11_USAGE_DEFAULT;
	HR( Dev->CreateTexture3D( &tdesc3d, NULL, &tex3D ) );

	memset( &srvdesc, 0, sizeof(srvdesc) );
	srvdesc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
	srvdesc.Texture3D.MipLevels = 1;
	srvdesc.Texture3D.MostDetailedMip = 0;
	srvdesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
	HR( Dev->CreateShaderResourceView( tex3D, &srvdesc, &DeltaJ ) );

	memset( &uavdesc, 0, sizeof(uavdesc) );
	uavdesc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
	uavdesc.Texture3D.FirstWSlice = 0;
	uavdesc.Texture3D.MipSlice = 0;
	uavdesc.Texture3D.WSize = RES_R;
	HR( Dev->CreateUnorderedAccessView( tex3D, &uavdesc, &DeltaJ_UAV ) );

	::CompileFromFile( "Modules\\D3D11Shaders\\PAS_copyInscatterN.fx", NULL, NULL, "CS_copyInscatterN", cs_ver, ShaderCompileFlag, 0, NULL, &SBlob, &EBlob, &hr );
	ShowShaderCompilationError( hr, EBlob );
	HR( Dev->CreateComputeShader( SBlob->GetBufferPointer(), SBlob->GetBufferSize(), NULL, &CS_DeltaJ ) );
	REL( SBlob );
	REL( EBlob );

	//========================================================
	//				Precompute
	//========================================================

	const UINT ClearValues[4] = { 0 };

	iCtx->ClearUnorderedAccessViewUint( Transmittance_UAV, ClearValues );

	const UINT UAVInitCounts = 0;

	//compute transmittance 2D texture T (line 1 in algorithm 4.1)
	iCtx->CSSetShader( CS_Transmittance, NULL, 0 );
	iCtx->CSSetConstantBuffers( 0, 1, &Precompute_const );
	iCtx->CSSetUnorderedAccessViews( 0, 1, &Transmittance_UAV, &UAVInitCounts );
	iCtx->Dispatch( 1, 1, 1 );

	//compute irradiance 2D texture deltaE (line 2 in algorithm 4.1)
	iCtx->CSSetShader( CS_Irradiance1, NULL, 0 );
	iCtx->CSSetUnorderedAccessViews( 0, 1, &DeltaE_UAV, &UAVInitCounts );
	iCtx->CSSetShaderResources( 0, 1, &Transmittance );
	iCtx->CSSetSamplers( 0, 1, &SS_Clamp );
	iCtx->Dispatch( 1, 1, 1 );

	//compute single scaterring texture deltaS (line 3 in algorithm 4.1)
	//Rayleigh and Mie separated in deltaSR + deltaSM
	iCtx->CSSetShader( CS_Inscatter1, NULL, 0 );
	iCtx->CSSetUnorderedAccessViews( 0, 1, &DeltaSR_UAV, &UAVInitCounts );
	iCtx->CSSetUnorderedAccessViews( 1, 1, &DeltaSM_UAV, &UAVInitCounts );
	//transmittance is already set
	//sampler is already set
	iCtx->Dispatch( 1, 1, 1 );

	//copies deltaE into irradiance texture E (k == 0.0)
	//...

	//copies deltaS into inscatter texture S (line 5 in algorithm 4.1)
	iCtx->CSSetShader( CS_copyInscatter1, NULL, 0 );
	iCtx->CSSetUnorderedAccessViews( 0, 1, &Inscatter_UAV, &UAVInitCounts );
	iCtx->CSSetShaderResources( 0, 1, &DeltaSR );
	iCtx->CSSetShaderResources( 1, 1, &DeltaSM );
	iCtx->Dispatch( 1, 1, 1 );

	// loop for each scattering order (line 6 in algorithm 4.1)
	for( UINT order = 2; order <= 4; ++order ) {

		// computes deltaJ (line 7 in algorithm 4.1)
		iCtx->CSSetShader( CS_DeltaJ, NULL, 0 );
		iCtx->CSSetUnorderedAccessViews( 0, 1, &DeltaJ_UAV, &UAVInitCounts );
		iCtx->CSSetShaderResources( 0, 1, &Transmittance );
		iCtx->CSSetShaderResources( 1, 1, &DeltaSR );
		iCtx->CSSetShaderResources( 2, 1, &DeltaSM );
		iCtx->CSSetShaderResources( 3, 1, &DeltaE );
		iCtx->Dispatch( 1, 1, 1 );

		// computes deltaE (line 8 in algorithm 4.1)
		iCtx->CSSetShader( CS_IrradianceN, NULL, 0 );
		iCtx->CSSetUnorderedAccessViews( 0, 1, &DeltaE_UAV, &UAVInitCounts );
		//update order in cbuffer...
		iCtx->Dispatch( 1, 1, 1 );

		// computes deltaS (line 9 in algorithm 4.1)
		iCtx->CSSetShader( CS_InscatterN, NULL, 0 );
		iCtx->CSSetUnorderedAccessViews( 0, 1, &DeltaSR_UAV, &UAVInitCounts );
		iCtx->CSSetShaderResources( 1, 1, &DeltaJ );
		iCtx->Dispatch( 1, 1, 1 );
	}
}*/